#include "MenuManager.h"
#include <limits>
#include <iostream>
using namespace std;

string username;
string password;
string role;

// 显示主菜单
void MenuManager::showMainMenu()
{
    while (true)
    {
        cout << "欢迎使用智能家居管理系统" << endl;
        cout << "1.登录" << endl;
        cout << "2.登出" << endl;
        cout << "3.注册用户" << endl;
        cout << "4.重置密码" << endl;
        cout << "5.修改密码" << endl;
        cout << "6.删除用户" << endl;
        cout << "7.更改个人信息" << endl;
        cout << "8.查询用户信息" << endl;
        cout << "请输入选择: " << endl;
        int option;
        cin >> option;
        // 检查输入是否合法
        if (cin.fail())
        {
            cin.clear();                                         // 清除错误标志
            cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略当前行的所有字符
            cout << "非法输入，请输入数字!" << endl;
            continue; // 重新进入循环
        }
        switch (option)
        {
        case 1:
            loginMenu();
            break;

        case 2:
            cout << "退出系统" << endl;
            return;

        default:
            cout << "非法输入!" << endl;
            break;
        }
    }
}

// 登录菜单
void MenuManager::loginMenu()
{
    UserManager user;
    while (true)
    {
        cout << "登录菜单" << endl;
        cout << "1.管理员登录" << endl;
        cout << "2.用户登录" << endl;
        cout << "3.返回上一级" << endl;
        cout << "请输入选择: " << endl;
        int option;
        cin >> option;
        // 检查输入是否合法
        if (cin.fail())
        {
            cin.clear();                                         // 清除错误标志
            cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 忽略当前行的所有字符
            cout << "非法输入，请输入数字!" << endl;
            continue;
        }
        switch (option)
        {
        case 1:
            user.setRole("admin");
            cout << "请输入管理员用户名: " << endl;
            cin >> username;
            user.setUsername(username);
            cout << "请输入管理员密码: " << endl;
            cin >> password;
            user.setPassword(password);
            if (userManager.login(&user) == 0)
            {
                cout << "登录成功" << endl;
                // TODO
            }
            break;
        case 2:
            break;
        case 3:
            return;
        default:
            cout << "非法输入!" << endl;
            break;
        }
    }
}